home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / Prog / U-Z / Viewer.src.cpt / Viewer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-12-31  |  12.3 KB  |  654 lines  |  [TEXT/KAHL]

  1. /* Viewer DA
  2.     View large text files
  3.     
  4.     Originally written in MegaMax C™,
  5.     converted to LightspeedC™ December '87
  6.     
  7.     by Steven R. Costenoble
  8. */
  9.  
  10. #include <DeviceMgr.h>
  11. #include <ControlMgr.h>
  12. #include <MenuMgr.h>
  13. #include <WindowMgr.h>
  14. #include <FontMgr.h>
  15. #include <EventMgr.h>
  16. #include <ToolboxUtil.h>
  17. #include <DialogMgr.h>
  18. #include "FileDisplay.h"
  19. #include "LineFile.h"
  20.  
  21. #define NULL 0L
  22.  
  23. enum { Open, Prime, Control, Status, Close };
  24.  
  25. #define OWNEDID(D)    ( 0xc000 | ~(D)->dCtlRefNum << 5 )
  26.  
  27. #define MENUBARHEIGHT    21
  28. #define H_INSET            25
  29. #define V_INSET            25
  30.  
  31. #define genalert    0
  32.  
  33. #define tabdialog    1
  34. #define tabitem        3
  35.  
  36. #define aboutdialog    2
  37.  
  38. #define openitem    1        /* menu items */
  39. #define setitem        3
  40. #define quititem    5
  41. #define aboutitem    7
  42.  
  43. /* globals for ease & call-back functions: */
  44.  
  45. ControlHandle    v_sbar, h_sbar;
  46. FDHandle        hFD;
  47. int                font_width, ownedid;
  48. MenuHandle        my_menu;
  49.  
  50. void accopen();
  51. void accctl();
  52. void accclose();
  53.  
  54. main( pb, dce, what )
  55. cntrlParam *pb;
  56. DCtlPtr dce;
  57. int what;
  58. {
  59.     if ( dce->dCtlStorage == NULL ) {
  60.         if ( what == Open )
  61.             GenError( "\pProblem opening Viewer DA, quitting",
  62.                       "\p", "\p", "\p" );
  63.         return 0;
  64.         }
  65.  
  66.     switch ( what ) {
  67.         case Open:        accopen( dce ); break;
  68.         case Control:    accctl( dce, pb ); break;
  69.         case Close:        accclose( dce ); break;
  70.         }
  71.  
  72.     return 0;
  73. }
  74.  
  75. void movesbars( win )
  76. register WindowPtr win;
  77. {
  78.     Rect goodrect;
  79.     register ControlHandle sbar;
  80.  
  81.     sbar = v_sbar;
  82.     
  83.     HideControl( sbar );
  84.     MoveControl( sbar, win->portRect.right - 15,
  85.                        win->portRect.top - 1 );
  86.     SizeControl( sbar, 16,
  87.                         win->portRect.bottom - win->portRect.top - 13 );
  88.     ShowControl( sbar );
  89.     goodrect = (**sbar).contrlRect;
  90.     ValidRect( &goodrect );
  91.  
  92.     sbar = h_sbar;
  93.  
  94.     HideControl( sbar );
  95.     MoveControl( sbar, win->portRect.left - 1,
  96.                        win->portRect.bottom - 15 );
  97.     SizeControl( sbar,
  98.                  win->portRect.right - win->portRect.left - 13, 16 );
  99.     ShowControl( sbar );
  100.     goodrect = (**sbar).contrlRect;
  101.     ValidRect( &goodrect );
  102. }
  103.  
  104. Boolean doopen( mywindow, tabs )
  105. WindowPeek mywindow;
  106. int tabs;
  107. {
  108.     LFile    file;
  109.     char    name[ 256 ];
  110.     Rect    text_bounds;
  111.     GrafPtr    oldport;
  112.  
  113.     LineFile( LF_Open, &file, name );
  114.     if ( file.size == 0 ) return FALSE;
  115.  
  116.     FileDisplay( FD_Dispose, hFD );
  117.  
  118.     SetWTitle( mywindow, name );
  119.  
  120.     text_bounds = mywindow->port.portRect;
  121.     text_bounds.right -= 15;
  122.     text_bounds.bottom -= 15;
  123.     
  124.     FileDisplay( FD_New, &hFD, &file, &text_bounds, (long) tabs );
  125.     
  126.     GetPort( &oldport );
  127.     SetPort( mywindow );
  128.     InvalRect( &text_bounds );
  129.     SetPort( oldport );
  130.     
  131.     SetCtlValue( h_sbar, 0 );
  132.     SetCtlValue( v_sbar, 0 );
  133.  
  134.     return TRUE;
  135. }
  136.  
  137. void accopen( dce )
  138. DCtlPtr dce;
  139. {
  140.     register WindowPeek    mywindow;
  141.     GrafPtr                oldport, wport;
  142.     FontInfo            info;
  143.     Rect                win_bounds;
  144.     
  145.     ownedid = OWNEDID( dce );
  146.     dce->dCtlFlags |= dNeedGoodBye;
  147.     dce->dCtlMenu = ownedid;
  148.  
  149.     if ( dce->dCtlWindow != NULL ) return;
  150.  
  151.     GetPort( &oldport );
  152.  
  153.     GetWMgrPort( &wport );
  154.     win_bounds = wport->portRect;
  155.     win_bounds.top += MENUBARHEIGHT;
  156.     InsetRect( &win_bounds, H_INSET, V_INSET );
  157.  
  158.     mywindow = (WindowPeek) NewWindow( NULL, &win_bounds, "\pViewer",
  159.                     FALSE, documentProc + 8, -1L, TRUE, 0L );
  160.     
  161.     if ( mywindow == NULL ) {
  162.         GenError( "\pProblem opening Viewer DA, quitting",
  163.                   "\p", "\p", "\p" );
  164.         return;
  165.         }
  166.  
  167.     mywindow->windowKind = dce->dCtlRefNum;
  168.     dce->dCtlWindow = (WindowPtr) mywindow;
  169.     
  170.     SetPort( mywindow );
  171.     
  172.     TextFont( monaco );
  173.     TextSize( 9 );
  174.     
  175.     GetFontInfo( &info );
  176.     font_width = info.widMax;
  177.         
  178.     v_sbar = NewControl( mywindow, &win_bounds, "\p", FALSE, 0, 0, 99,
  179.                 scrollBarProc, 0L );
  180.  
  181.     h_sbar = NewControl( mywindow, &win_bounds, "\p", FALSE, 0, 0, 255,
  182.                 scrollBarProc, 0L );
  183.  
  184.     movesbars( mywindow );
  185.  
  186.     hFD = NULL;
  187.  
  188.     if ( !doopen( mywindow, 4 ) ) {
  189.         DisposeWindow( mywindow );
  190.         dce->dCtlWindow = NULL;
  191.         return;
  192.         }
  193.  
  194.     my_menu = NewMenu( ownedid, "\pViewer" );
  195.     AppendMenu( my_menu, "\pOpen…;(-;Set Tabs…;(-;Quit;(-;About Viewer…" );
  196.     
  197.     SetPort( oldport );
  198.     
  199.     return;
  200. }
  201.  
  202. void doshutdown( mywindow )
  203. WindowPeek mywindow;
  204. {
  205.     FileDisplay( FD_Dispose, hFD );
  206.     DisposeWindow( mywindow );
  207.     HiliteMenu( 0 );
  208.     DeleteMenu( ownedid );
  209.     DisposeMenu( my_menu );
  210.     DrawMenuBar();
  211. }
  212.  
  213. void accclose( dce )
  214. DCtlPtr dce;
  215. {
  216.     register WindowPtr mywindow;
  217.     
  218.     if ( ( mywindow = dce->dCtlWindow ) != NULL )
  219.         doshutdown( mywindow );
  220.  
  221.     dce->dCtlWindow = NULL;
  222.  
  223.     return;
  224. }
  225.  
  226. void dokeydown( theEvent )
  227. register EventRecord *theEvent;
  228. {
  229.     if ( ( theEvent->modifiers & cmdKey ) &&
  230.          ( ( theEvent->message & charCodeMask ) == 'c' ||
  231.            ( theEvent->message & charCodeMask ) == 'C' ) )
  232.         FileDisplay( FD_Copy, hFD );
  233. }
  234.  
  235. void doactivate( mywindow, theEvent )
  236. WindowPeek mywindow;
  237. EventRecord *theEvent;
  238. {
  239.     if ( theEvent->modifiers & activeFlag ) {
  240.         ShowControl( v_sbar );                /* activate */
  241.         ShowControl( h_sbar );
  242.         FileDisplay( FD_Activate, hFD, 1L );
  243.         InsertMenu( my_menu, 0 );
  244.         }
  245.  
  246.     else {
  247.         HideControl( v_sbar );                /* deactivate */
  248.         HideControl( h_sbar );
  249.         FileDisplay( FD_Activate, hFD, 0L );
  250.         DeleteMenu( ownedid );
  251.         }
  252.  
  253.     DrawGrowIcon( mywindow );
  254.     DrawMenuBar();
  255. }
  256.  
  257. void doupdate( mywindow )
  258. register WindowPtr mywindow;
  259. {
  260.     BeginUpdate( mywindow );
  261.     
  262.     EraseRect( &mywindow->portRect );
  263.     UpdtControl( mywindow, mywindow->visRgn );
  264.     DrawGrowIcon( mywindow );
  265.     
  266.     FileDisplay( FD_Update, hFD );
  267.     
  268.     EndUpdate( mywindow );
  269. }
  270.  
  271. void docursor( mywindow )
  272. WindowPtr mywindow;
  273. {
  274.     Rect    text_rect;
  275.     Point    where;
  276.     register CursHandle    icurs;
  277.     
  278.     text_rect = mywindow->portRect;
  279.     text_rect.right -= 15;
  280.     text_rect.bottom -= 15;
  281.     
  282.     GetMouse( &where );
  283.     if ( PtInRect( where, &text_rect ) ) {
  284.         icurs = GetCursor( iBeamCursor );
  285.         SetCursor( *icurs );
  286.         }
  287.     else
  288.         InitCursor();
  289.  
  290.     FileDisplay( FD_Idle, hFD );
  291. }
  292.  
  293. void doset()
  294. {
  295.     register DialogPtr    dlog;
  296.  
  297.     int            itemhit, itemtype;
  298.     long        newtab;
  299.     Handle        thetext;
  300.     Rect        itembox;
  301.     char        tabstr[ 256 ];
  302.  
  303.     dlog = GetNewDialog( ownedid + tabdialog, NULL, -1L );
  304.  
  305.     NumToString( (long) (**hFD).tabsize, tabstr );
  306.  
  307.     GetDItem( dlog, tabitem, &itemtype, &thetext, &itembox );
  308.     SetIText( thetext, tabstr );
  309.     SelIText( dlog, tabitem, 0, 32767 );
  310.     
  311.     ShowWindow( dlog );
  312.     ModalDialog( NULL, &itemhit );
  313.     
  314.     if ( itemhit == OK ) {
  315.         GetIText( thetext, tabstr );
  316.         StringToNum( tabstr, &newtab );
  317.         FileDisplay( FD_SetTabs, hFD, newtab );
  318.         }
  319.  
  320.     DisposDialog( dlog );
  321. }
  322.  
  323. void domenu( mident, item, mywindow )
  324. int mident, item;
  325. WindowPeek mywindow;
  326. {
  327.     switch ( item ) {
  328.         case openitem:
  329.             doopen( mywindow, (**hFD).tabsize );
  330.             break;
  331.  
  332.         case setitem:
  333.             doset();
  334.             break;
  335.  
  336.         case quititem:
  337.             CloseDeskAcc( mywindow->windowKind );
  338.             break;
  339.  
  340.         case aboutitem:
  341.             Alert( ownedid + aboutdialog, NULL );
  342.             break;
  343.         }
  344.     DrawMenuBar();
  345. }
  346.  
  347. void dogrow( mywindow, where )
  348. register WindowPtr mywindow;
  349. Point where;
  350. {
  351.     union {                /* used to interpret growwindow return value */
  352.         long retval;
  353.         struct { int ht, wd; } dim;
  354.         } newsize;
  355.  
  356.     Rect sizerect, textrect, goodrect;
  357.  
  358.     sizerect.top = sizerect.left = 70;
  359.     sizerect.right = sizerect.bottom = 32000;
  360.     
  361.     if ( ( newsize.retval = GrowWindow( mywindow, where, &sizerect ) )
  362.             != 0L ) {
  363.         InvalRect( &mywindow->portRect );
  364.         SizeWindow( mywindow, newsize.dim.wd, newsize.dim.ht, TRUE );
  365.         movesbars( mywindow );
  366.  
  367.         goodrect = (**(**hFD).TE).viewRect;
  368.  
  369.         textrect = mywindow->portRect;
  370.         textrect.right -= 15;
  371.         textrect.bottom -= 15;
  372.         FileDisplay( FD_Resize, hFD, &textrect );
  373.         
  374.         SectRect( &goodrect, &(**(**hFD).TE).viewRect, &goodrect );
  375.         ValidRect( &goodrect );
  376.         }
  377. }
  378.  
  379. void dozoom( win, where, part )
  380. register WindowPtr win;
  381. Point where;
  382. int part;
  383. {
  384.     Rect textrect;
  385.  
  386.     if ( TrackBox( win, where, part ) ) {
  387.         EraseRect( &win->portRect );
  388.         InvalRect( &win->portRect );
  389.         ZoomWindow( win, part, FALSE );
  390.         movesbars( win );
  391.         
  392.         textrect = win->portRect;
  393.         textrect.right -= 15;
  394.         textrect.bottom -= 15;
  395.         FileDisplay( FD_Resize, hFD, &textrect );
  396.         }
  397. }
  398.  
  399. pascal v_action( sbar, part )
  400. ControlHandle sbar;
  401. int part;
  402. {
  403.     long pos;
  404.     int oldpercent, newpercent;
  405.     register FDHandle HFD;
  406.  
  407.     SetUpA4();
  408.  
  409.     HFD = hFD;
  410.  
  411.     oldpercent = GetCtlValue( sbar );
  412.     pos = (**HFD).filestarts[ 0 ];
  413.  
  414.     switch ( part ) {
  415.  
  416.         case inUpButton:
  417.             if ( pos == 0L ) break;
  418.             FileDisplay( FD_Scroll, HFD, 0L, 1L, &pos );
  419.             break;
  420.  
  421.         case inDownButton:
  422.             if ( (**HFD).actuallines < (**HFD).maxlines ) break;
  423.             FileDisplay( FD_Scroll, HFD, 0L, -1L, &pos );
  424.             break;
  425.  
  426.         case inPageUp:
  427.             if ( pos > 0L )
  428.                 FileDisplay( FD_Redraw, HFD, &pos,
  429.                                 (long) -(**HFD).maxlines + 1 );
  430.             break;
  431.         
  432.         case inPageDown:
  433.             if ( (**HFD).actuallines == (**HFD).maxlines ) {
  434.                 pos = (**HFD).filestarts[ (**HFD).actuallines - 1 ];
  435.                 FileDisplay( FD_Redraw, HFD, &pos, 0L );
  436.                 }
  437.             break;
  438.         }
  439.  
  440.     newpercent = ( pos * 100 ) / (**HFD).file.size;
  441.     if ( newpercent != oldpercent )
  442.         SetCtlValue( sbar, newpercent );
  443.  
  444.     RestoreA4();
  445. }
  446.  
  447. void doVscroll( part, where )
  448. int part;
  449. Point where;
  450. {
  451.     register int percent;
  452.     long pos;
  453.     register ControlHandle sbar = v_sbar;
  454.  
  455.     if ( part == inThumb ) {
  456.         if ( TrackControl( sbar, where, NULL ) ) {
  457.             percent = GetCtlValue( sbar );
  458.             pos = ( (**hFD).file.size * percent ) / 100;
  459.             FileDisplay( FD_Redraw, hFD, &pos, 0L );
  460.             percent = ( pos * 100 ) / (**hFD).file.size;
  461.             SetCtlValue( sbar, percent );
  462.             }
  463.         }
  464.     else
  465.         TrackControl( sbar, where, v_action );
  466. }
  467.  
  468. pascal h_action( sbar, part )
  469. register ControlHandle sbar;
  470. int part;
  471. {
  472.     register int oldcharpos, newcharpos;
  473.     register long pixmove;
  474.     register TEHandle hTE;
  475.     long dummy;
  476.     
  477.     SetUpA4();
  478.     
  479.     hTE = (**hFD).TE;
  480.     
  481.     newcharpos = oldcharpos = GetCtlValue( sbar );
  482.  
  483.     switch ( part ) {
  484.  
  485.         case inUpButton:
  486.             if ( oldcharpos > 0 )
  487.                 newcharpos -= 1;
  488.             break;
  489.         
  490.         case inDownButton:
  491.             if ( oldcharpos < GetCtlMax( sbar ) )
  492.                 newcharpos += 1;
  493.             break;
  494.  
  495.         case inPageUp:
  496.             newcharpos -= 40;
  497.             if ( newcharpos < 0 )
  498.                 newcharpos = 0;
  499.             break;
  500.  
  501.         case inPageDown:
  502.             newcharpos += 40;
  503.             if ( newcharpos > GetCtlMax( sbar ) )
  504.                 newcharpos = GetCtlMax( sbar );
  505.             break;
  506.         }
  507.     
  508.     pixmove = (**hTE).viewRect.left - (**hTE).destRect.left
  509.                 - newcharpos * font_width + BORDER;
  510.     if ( pixmove )
  511.         FileDisplay( FD_Scroll, hFD, pixmove, 0L, &dummy );
  512.     if ( newcharpos != oldcharpos )
  513.         SetCtlValue( sbar, newcharpos );
  514.  
  515.     RestoreA4();
  516. }
  517.  
  518. void doHscroll( part, where )
  519. int part;
  520. Point where;
  521. {
  522.     register long pixmove;
  523.     register TEHandle hTE = (**hFD).TE;
  524.     register ControlHandle sbar = h_sbar;
  525.     long dummy;
  526.     
  527.     if ( part == inThumb ) {
  528.         if ( TrackControl( sbar, where, NULL ) ) {
  529.             pixmove = (**hTE).viewRect.left - (**hTE).destRect.left
  530.                         - GetCtlValue( sbar ) * font_width + BORDER;
  531.             FileDisplay( FD_Scroll, hFD, pixmove, 0L, &dummy );
  532.             }
  533.         }
  534.     else
  535.         TrackControl( sbar, where, h_action );
  536. }
  537.  
  538. void docontent( mywindow, theEvent )
  539. WindowPtr mywindow;
  540. EventRecord *theEvent;
  541. {
  542.     ControlHandle sbar;
  543.     register int part;
  544.     Point where;
  545.     
  546.     where = theEvent->where;
  547.     GlobalToLocal( &where );
  548.     
  549.     part = FindControl( where, mywindow, &sbar );
  550.     
  551.     if ( part != 0 )
  552.         if ( sbar == v_sbar )
  553.             doVscroll( part, where );
  554.         else
  555.             doHscroll( part, where );
  556.     else
  557.         FileDisplay( FD_Click, hFD, theEvent );
  558. }
  559.  
  560. void domousedown( mywindow, theEvent )
  561. register WindowPeek mywindow;
  562. register EventRecord *theEvent;
  563. {
  564.     WindowPtr    whichwindow;
  565.     int            partcode, kind;
  566.     
  567.     kind = mywindow->windowKind;
  568.     mywindow->windowKind = userKind;    /* fool findwindow */
  569.  
  570.     switch ( partcode = FindWindow( theEvent->where, &whichwindow ) ) {
  571.  
  572.         case inContent:
  573.             docontent( mywindow, theEvent );
  574.             break;
  575.  
  576.         case inGrow:
  577.             dogrow( mywindow, theEvent->where );
  578.             break;
  579.  
  580.         case inZoomIn:
  581.         case inZoomOut:
  582.             dozoom( mywindow, theEvent->where, partcode );
  583.             break;
  584.         }
  585.  
  586.     mywindow->windowKind = kind;
  587. }
  588.  
  589. void accctl( dce, pb )
  590. DCtlPtr dce;
  591. cntrlParam *pb;
  592. {
  593.     register EventRecord    *theEvent;
  594.     register WindowPtr        mywindow;
  595.     GrafPtr                    oldport;
  596.     
  597.     if ( ( mywindow = dce->dCtlWindow ) == NULL ) return;
  598.     GetPort( &oldport );
  599.     SetPort( mywindow );
  600.     
  601.     switch ( pb->csCode ) {
  602.  
  603.         case accEvent:
  604.             theEvent =  *(EventRecord **) pb->csParam;
  605.  
  606.             switch ( theEvent->what ) {
  607.  
  608.                 case mouseDown:
  609.                     domousedown( mywindow, theEvent );
  610.                     break;
  611.  
  612.                 case keyDown:
  613.                 case autoKey:
  614.                     dokeydown( theEvent );
  615.                     break;
  616.  
  617.                 case activateEvt:
  618.                     doactivate( mywindow, theEvent );
  619.                     break;
  620.  
  621.                 case updateEvt:
  622.                     doupdate( mywindow );
  623.                     break;
  624.                 }
  625.             break;
  626.  
  627.         case accCursor:
  628.             docursor( mywindow );
  629.             break;
  630.  
  631.         case accMenu:
  632.             domenu( pb->csParam[0], pb->csParam[1], mywindow );
  633.             break;
  634.  
  635.         case accCopy:
  636.             FileDisplay( FD_Copy, hFD );
  637.             break;
  638.  
  639.         case goodBye:
  640.             doshutdown( mywindow );
  641.             break;
  642.         }
  643.  
  644.     SetPort( oldport );
  645.     return;
  646. }
  647.  
  648. GenError( str1, str2, str3, str4 )
  649. char str1[], str2[], str3[], str4[];
  650. {
  651.     ParamText( str1, str2, str3, str4 );
  652.     StopAlert( ownedid + genalert, NULL );
  653. }
  654.